package com.lcxw.hxzbapi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lcxw.hxzbapi.dao.mapper.PagingMapper;
import com.lcxw.hxzbapi.entity.pojo.Paging;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class PagingService {

    @Resource
    PagingMapper pagingMapper;

    public int insert(Paging paging) {
        return pagingMapper.insert(paging);
    }

    public Paging selectByUserId(long userId) {
        QueryWrapper<Paging> wrapper = new QueryWrapper<>();
        wrapper.eq("userId", userId);
        return pagingMapper.selectOne(wrapper);
    }

    public Paging selectByRoomId(long roomId) {
        QueryWrapper<Paging> wrapper = new QueryWrapper<>();
        wrapper.eq("roomId", roomId);
        return pagingMapper.selectOne(wrapper);
    }

    public List<Paging> getALL() {
        return pagingMapper.selectList(null);
    }

    //获取有效的寻人广播
    public List<Paging> getAlive() {
        Date now = new Date();
        Date now_10 = new Date(now.getTime() - 600000); //10分钟前的时间
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
        String nowTime_10 = dateFormat.format(now_10);
        QueryWrapper<Paging> wrapper = new QueryWrapper<>();
        wrapper.gt("addTime", nowTime_10);
        wrapper.orderByDesc("addTime");
        return pagingMapper.selectList(wrapper);
    }

    //获取过期的寻人广播
    public List<Paging> getExpire() {
        Date now = new Date();
        Date now_10 = new Date(now.getTime() - 600000); //10分钟前的时间
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
        String nowTime_10 = dateFormat.format(now_10);
        QueryWrapper<Paging> wrapper = new QueryWrapper<>();
        wrapper.le("addTime", nowTime_10);
        return pagingMapper.selectList(wrapper);
    }

    public int deleteById(Integer id) {
        return pagingMapper.deleteById(id);
    }

    public int update(Paging paging) {
        return pagingMapper.updateById(paging);
    }


}
